﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Collections;

namespace PJOnimal
{
    public class Background
    {
        Texture2D[] nen;
        int nnen;
        public Vector2[] toaDo = new Vector2[10];
        public Vector2 toaDoNhan; //nhân vật đến gần mới load để tránh lag
        public ArrayList lstBackground = new ArrayList();
        public Background(Onimal game, Vector2 toaDoNhanVat)
        {
            nnen = 1;
            nen = new Texture2D[nnen ];
            nen[0] = game.Content.Load<Texture2D>("HinhNen/HinhNen3");

            //load tọa độ
            toaDo[0] = Vector2.Zero;
            lstBackground.Add(new Sprite(nen, 1, toaDo[0], 1));
            toaDo[1] = new Vector2(800, 0);
            lstBackground.Add(new Sprite(nen, 1, toaDo[1], 1));
            toaDo[2] = new Vector2(1600, 0);
            lstBackground.Add(new Sprite(nen, 1, toaDo[2], 1));
            toaDo[3] = new Vector2(2400, 0);
            lstBackground.Add(new Sprite(nen, 1, toaDo[3], 1));
            toaDo[4] = new Vector2(3200, 0);
            lstBackground.Add(new Sprite(nen, 1, toaDo[4], 1));
            toaDo[5] = new Vector2(4000, 0);
            lstBackground.Add(new Sprite(nen, 1, toaDo[5], 1));
            toaDo[6] = new Vector2(4800, 0);
            lstBackground.Add(new Sprite(nen, 1, toaDo[6], 1));
            toaDo[7] = new Vector2(5600, 0);
            lstBackground.Add(new Sprite(nen, 1, toaDo[7], 1));
            toaDo[8] = new Vector2(6400, 0);
            lstBackground.Add(new Sprite(nen, 1, toaDo[8], 1));
            toaDo[9] = new Vector2(7200, 0);
            lstBackground.Add(new Sprite(nen, 1, toaDo[9], 1));

            //load character
            nnen = 6;
            nen = new Texture2D[nnen];
            for (int i = 0; i < nnen; i++)
            {
                nen[i] = game.Content.Load<Texture2D>("HinhNen/Leaf/" + (i+1).ToString("00"));
            }
            lstBackground.Add(new Sprite(nen, nnen, new Vector2(500, 200), 1));
            this.toaDoNhan = toaDoNhanVat;
        }
        public void UpdateFlag(GameTime gameTime)
        {
            for (int i = 0; i<lstBackground.Count ; i++)
            {                
                ((Sprite)lstBackground[i]).FlagAction +=1;
                if (((Sprite)lstBackground[i]).FlagAction == ((Sprite)lstBackground[i]).NTexture2d)
                {
                    ((Sprite)lstBackground[i]).FlagAction = 0;
                }
                ((Sprite)lstBackground[i]).UpdateAnimationBacground(gameTime);
            }
        }
        public void Draw(SpriteBatch sp)
        {
            for (int i = 0; i<lstBackground.Count ; i++)
            {
                //if (Vector2.Distance(toaDoNhan, toaDo[i]) < 1500)
                    ((Sprite)lstBackground[i]).DrawBackGround(sp);
            }
        }
    }
}
